package org.gnyxy.controller.background;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.constant.JwtClaimsConstant;
import org.gnyxy.context.BaseContext;
import org.gnyxy.dto.AdminDTO;
import org.gnyxy.dto.AdminNotPasswordDTO;
import org.gnyxy.dto.AdminPageQueryDTO;
import org.gnyxy.dto.PasswordUpdateRequest;
import org.gnyxy.entity.Admin;
import org.gnyxy.properties.JwtProperties;
import org.gnyxy.result.PageResult;
import org.gnyxy.result.Result;
import org.gnyxy.service.AdminService;
import org.gnyxy.utils.JwtUtil;
import org.gnyxy.vo.AdminSelectVO;
import org.gnyxy.vo.AdminVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Objects;

@RestController
@RequestMapping("/background/admin")
@Slf4j
@Api(tags = "商家后台-管理员")
//@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 管理员登录
     * @param adminDTO
     * @return adminVO
     */
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    public Result<AdminVO> login(@RequestBody AdminDTO adminDTO){
        log.info("管理员登录: {}",adminDTO);

        Admin admin = adminService.login(adminDTO);

        HashMap<String, Object> map = new HashMap<>();
        // 存入map
        map.put(JwtClaimsConstant.ADMIN_ID,admin.getId());
        // 生成 token
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                map);

        AdminVO adminVO = AdminVO.builder()
                .id(admin.getId())
                .name(admin.getName())
                .token(token)
                .build();
        return Result.success(adminVO);
    }

    /**
     * 退出登录
     * @return null
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result<String> logout() {
        BaseContext.removeCurrentId();
        return Result.success();
    }

    /**
     * 新增管理员
     * @param adminDTO
     * @return null
     */
    @PostMapping("/add")
    @ApiOperation("新增管理员")
    public Result add(@RequestBody AdminDTO adminDTO){
        log.info("新增管理员: {}",adminDTO);
        adminService.add(adminDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param adminPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> getPage(AdminPageQueryDTO adminPageQueryDTO){
        log.info("分页查询: {}",adminPageQueryDTO);
        PageResult pageResult = adminService.getPage(adminPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改管理员
     * @param adminNotPasswordDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改管理员")
    public Result update(@RequestBody AdminNotPasswordDTO adminNotPasswordDTO){
        log.info("修改用户: {}",adminNotPasswordDTO);
        adminService.update(adminNotPasswordDTO);
        return Result.success();
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除管理员")
    public Result delete(@PathVariable Long id){
        log.info("删除管理员id: {}",id);
        adminService.delete(id);
        return Result.success();
    }

    /**
     * 根据id查找管理员
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查找管理员")
    public Result<AdminSelectVO> getById(@PathVariable Long id){
        log.info("查找管理员: {}",id);
        Admin admin = adminService.getById(id);
        AdminSelectVO adminSelectVO = new AdminSelectVO();
        BeanUtils.copyProperties(admin,adminSelectVO);
        return Result.success(adminSelectVO);
    }

    /**
     * 修改管理员密码
     * @param passwordUpdateRequest
     * @return
     */
    @PostMapping("/setPassword")
    @ApiOperation("修改密码")
    public Result setPassword(@RequestBody PasswordUpdateRequest passwordUpdateRequest){
        log.info("传入的新密码是：{}",passwordUpdateRequest);
        adminService.setPassword(passwordUpdateRequest.getPassword());
        return Result.success();
    }
}
